package com.ruoyi.web.controller.module.titleMenu;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.location.City;
import com.ruoyi.system.domain.module.location.Province;
import com.ruoyi.system.domain.module.titleMenu.TitleMenuSectionCity;
import com.ruoyi.system.service.module.location.CityService;
import com.ruoyi.system.service.module.location.ProvinceService;
import com.ruoyi.system.service.module.titleMenu.ITitleMenuSectionCityService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 行所显示的城市Controller
 *
 * @author ruoyi
 * @date 2022-12-10
 */
@Validated
@Api(value = "行所显示的城市控制器", tags = {"行所显示的城市管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/titleMenu/section/city")
public class TitleMenuSectionCityController extends BaseController {

    private final ITitleMenuSectionCityService titleMenuSectionCityService;
	private final ProvinceService provinceService;
	private final CityService cityService;

    /**
     * 查询行所显示的城市列表
     */
    @ApiOperation("查询行所显示的城市列表")
    @PreAuthorize("@ss.hasPermi('module:sectionsCity:list')")
    @GetMapping("/list")
    public TableDataInfo<TitleMenuSectionCity> list(@Validated TitleMenuSectionCity titleMenuSectionCity) {
        QueryWrapper<TitleMenuSectionCity> qw = new QueryWrapper<>();
        if(titleMenuSectionCity.getMenuSectionId()!=null){
        	qw.eq("menu_section_id",titleMenuSectionCity.getMenuSectionId());
		}

        TableDataInfo<TitleMenuSectionCity> td = PageUtils.buildDataInfo(titleMenuSectionCityService.page(PageUtils.buildPage(),qw));
        List<TitleMenuSectionCity> list = td.getRows();
        return td;
    }

    /**
     * 导出行所显示的城市列表
     */
    @ApiOperation("导出行所显示的城市列表")
    @PreAuthorize("@ss.hasPermi('module:sectionsCity:export')")
    @Log(title = "行所显示的城市", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TitleMenuSectionCity> export(@Validated TitleMenuSectionCity titleMenuSectionCity) {
        List<TitleMenuSectionCity> list = titleMenuSectionCityService.list();
        ExcelUtil<TitleMenuSectionCity> util = new ExcelUtil<TitleMenuSectionCity>(TitleMenuSectionCity.class);
        return util.exportExcel(list, "行所显示的城市");
    }

    /**
     * 获取行所显示的城市详细信息
     */
    @ApiOperation("获取行所显示的城市详细信息")
    @PreAuthorize("@ss.hasPermi('module:sectionsCity:query')")
    @GetMapping("/{id}")
    public AjaxResult<TitleMenuSectionCity> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        TitleMenuSectionCity titleMenuSectionCity = titleMenuSectionCityService.getById(id);
        if(titleMenuSectionCity!=null){
			String[] areas = { titleMenuSectionCity.getProvId()+"",titleMenuSectionCity.getCityId()+""};
			titleMenuSectionCity.setProvinceArray(areas);
		}
        return AjaxResult.success(titleMenuSectionCity);
    }

    /**
     * 新增行所显示的城市
     */
    @ApiOperation("新增行所显示的城市")
    @PreAuthorize("@ss.hasPermi('module:sectionsCity:add')")
    @Log(title = "行所显示的城市", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody TitleMenuSectionCity titleMenuSectionCity) {

		if(titleMenuSectionCity.getProvinceArray()!=null&&titleMenuSectionCity.getProvinceArray().length==2){

			Province province = provinceService.findById(titleMenuSectionCity.getProvinceArray()[0]);
			if(province!=null){
				titleMenuSectionCity.setProvName(province.getProvinceName());
				titleMenuSectionCity.setProvId(province.getId());
			}

			City city = cityService.findById(titleMenuSectionCity.getProvinceArray()[1]);
			if(city!=null){
				titleMenuSectionCity.setCityName(city.getCityName());
				titleMenuSectionCity.setCityId(city.getId());
			}
		}

		return toAjax(titleMenuSectionCityService.save(titleMenuSectionCity) ? 1 : 0);
    }

    /**
     * 修改行所显示的城市
     */
    @ApiOperation("修改行所显示的城市")
    @PreAuthorize("@ss.hasPermi('module:sectionsCity:edit')")
    @Log(title = "行所显示的城市", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody TitleMenuSectionCity titleMenuSectionCity) {

		if(titleMenuSectionCity.getProvinceArray()!=null&&titleMenuSectionCity.getProvinceArray().length==2){

			Province province = provinceService.findById(titleMenuSectionCity.getProvinceArray()[0]);
			if(province!=null){
				titleMenuSectionCity.setProvName(province.getProvinceName());
				titleMenuSectionCity.setProvId(province.getId());
			}

			City city = cityService.findById(titleMenuSectionCity.getProvinceArray()[1]);
			if(city!=null){
				titleMenuSectionCity.setCityName(city.getCityName());
				titleMenuSectionCity.setCityId(city.getId());
			}
		}
        return toAjax(titleMenuSectionCityService.updateById(titleMenuSectionCity) ? 1 : 0);
    }

    /**
     * 删除行所显示的城市
     */
    @ApiOperation("删除行所显示的城市")
    @PreAuthorize("@ss.hasPermi('module:sectionsCity:remove')")
    @Log(title = "行所显示的城市" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(titleMenuSectionCityService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
